package com.yuncheng.spcyApi.spcy;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.config.ConfigureBuilder;
import com.deepoove.poi.data.PictureRenderData;
import com.deepoove.poi.data.PictureType;
import com.deepoove.poi.data.Pictures;
import com.deepoove.poi.policy.HackLoopTableRenderPolicy;
import com.yuncheng.spcyApi.alog.annotation.Log;
import com.yuncheng.spcyApi.busService.BusService;
import com.yuncheng.spcyApi.busService.InitWjjlService;
import com.yuncheng.spcyApi.config.SpcyMinioUtils;
import com.yuncheng.spcyApi.constant.common.LogConstant;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.constant.wjml.WjmlConstant;
import com.yuncheng.spcyApi.entity.*;
import com.yuncheng.spcyApi.mapper.SpcySqsxBaiscMapper;
import com.yuncheng.spcyApi.mapper.SpcyXcjcBjddMapper;
import com.yuncheng.spcyApi.service.*;
import com.yuncheng.spcyApi.spcyService.SpcyService;
import com.yuncheng.spcyApi.utils.*;
import com.yuncheng.spcyApi.vo.xcjchy.XcjcBjmlAddVo;
import com.yuncheng.spcyApi.vo.xcjchy.XcjcBjmlVo;
import com.yuncheng.spcyApi.vo.xcjchy.XcjcWjjcqkAddVo;
import com.yuncheng.vo.HttpResult;
import com.yuncheng.spcyApi.vo.common.HttpResultVo;
import com.yuncheng.spcyApi.vo.xcjchy.XcjcBjddAddVo;
import io.swagger.annotations.*;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import utils.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author hua
 */
@Api(tags = "现场检查笔记相关接口")
@RestController
@RequestMapping("/api/spzx-spcy/jcbj")
public class SpcyXcjcbjController {

    private static final Logger log = LoggerFactory.getLogger(SpcyXcjcbjController.class);

    @Resource
    private InitWjjlService initWjjlService;

    @Resource
    private ISpcyXcjcBjddService spcyXcjcBjddService;

    @Resource
    private SpcyXcjcBjddMapper spcyXcjcBjddMapper;

    @Resource
    private ISpcyXcjcBjmlService spcyXcjcBjmlService;

    @Resource
    private ISpcySqsxWjjlService spcySqsxWjjlService;

    @Resource
    private LocalFileUploadUtils localFileUploadUtils;

    @Resource
    private SpcyService spcyService;

    @Resource
    private ISpcyLogService spcyLogService;

    @Resource
    private ISpcySqsxXpjlService spcySqsxXpjlService;

    @Resource
    private ISpcyXcjcWjjcqkService spcyXcjcWjjcqkService;

    @Resource
    private SpcySqsxBaiscMapper spcySqsxBaiscMapper;

    @Resource
    private SpcyMinioUtils spcyMinioUtils;

    @Resource
    private BusService busService;

    @Log(title = "现场检查笔记-检查笔记-人员目录")
    @ApiOperation(value = "现场检查笔记-检查笔记-人员目录")
    @GetMapping(value = "queryBjRyList")
    public HttpResult queryBjRyList(@RequestParam(value = "sqsxid",defaultValue = "") @ApiParam("申请事项id") String sqsxid){
        if (StringUtils.isBlank(sqsxid)){
            return HttpResult.error("事项参数为空！");
        }

        List<SpcySqsxXpjl> list = spcySqsxXpjlService.getTyXpXpjlList(sqsxid, "");
        List<Map> dataListMap = new ArrayList<>();
        Map resultMap = new HashMap();
        // 验证组长
        boolean isZZState = false;
        String currentUserId = GetCurrentUserUtils.getCurrentUserId();
        List<SpcySqsxXpjl> zzList = list.stream()
                .filter(f -> f.getFuserid().equals(currentUserId) && f.getFssk().equals(SpcyConstant.HCY_SSK_ZZ))
                .collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(zzList)){
            isZZState = true;
        }

        for (int i=0; i<list.size(); i++){
            // 非组长
            if (!isZZState && !list.get(i).getFuserid().equals(currentUserId)){
                continue;
            }

            Map par = new HashMap();

            par.put("fuserid", list.get(i).getFuserid());
            par.put("name", list.get(i).getFjcyxm()+"/"+list.get(i).getFssk());

            if (list.get(i).getFuserid().equals(currentUserId)){
                par.put("edit", true);
            }else {
                par.put("edit", false);
            }

            dataListMap.add(par);
        }

        resultMap.put("list", dataListMap);
        resultMap.put("isZZState", isZZState);  // true：为组长
        resultMap.put("nowUserId", GetCurrentUserUtils.getCurrentUserId());
        return HttpResult.ok(dataListMap);
    }

    @Log(title = "现场检查笔记-文件检查情况-列表")
    @ApiOperation(value = "现场检查笔记-文件检查情况-列表")
    @GetMapping(value = "queryJcqkList")
    public HttpResult queryJcqkList(@RequestParam(value = "sqsxid",defaultValue = "") @ApiParam("申请事项id") String sqsxid,
                                    @RequestParam(value = "fuserid",defaultValue = "") @ApiParam("用户id") String fuserid) {

        if (StringUtils.isBlank(sqsxid)) {
            return HttpResult.error("参数为空！");
        }

        if (StringUtils.isBlank(fuserid)) {
            return HttpResult.ok(spcyXcjcWjjcqkService.list(new LambdaQueryWrapper<SpcyXcjcWjjcqk>()
                    .eq(SpcyXcjcWjjcqk::getFsqsxid, sqsxid)
                    .orderByAsc(SpcyXcjcWjjcqk::getCreateTime)));
        } else {
            return HttpResult.ok(spcyXcjcWjjcqkService.list(new LambdaQueryWrapper<SpcyXcjcWjjcqk>()
                    .eq(SpcyXcjcWjjcqk::getFsqsxid, sqsxid)
                    .eq(SpcyXcjcWjjcqk::getFuserid, fuserid)
                    .orderByAsc(SpcyXcjcWjjcqk::getCreateTime)));
        }
    }

    @Log(title = "APP-检查笔记-文件检查情况")
    @ApiOperation(value = "APP-检查笔记-文件检查情况")
    @GetMapping(value = "queryAppJcqk")
    public HttpResult queryAppJcqk(@RequestParam(value = "sqsxid",defaultValue = "") @ApiParam("申请事项id") String sqsxid) {

        if (StringUtils.isBlank(sqsxid)) {
            return HttpResult.error("参数为空！");
        }

        return HttpResult.ok(spcyXcjcWjjcqkService.list(new LambdaQueryWrapper<SpcyXcjcWjjcqk>()
                .eq(SpcyXcjcWjjcqk::getFsqsxid, sqsxid)
                .eq(SpcyXcjcWjjcqk::getFuserid, GetCurrentUserUtils.getCurrentUserId())
                .orderByAsc(SpcyXcjcWjjcqk::getCreateTime)));
    }

    @Log(title = "APP-历史文件检查情况-文件分类")
    @ApiOperation(value = "APP-历史文件检查情况-文件分类")
    @GetMapping(value = "getLsJcqkfl")
    public HttpResult getLsJcqkfl(@RequestParam(value = "sqsxid",defaultValue = "0") @ApiParam("申请事项id") String sqsxid){

        return HttpResult.ok(spcyXcjcBjddMapper.selectHistoryFwjflBySqsxId(sqsxid));
    }

    @Log(title = "APP-查询地点车间历史")
    @ApiOperation(value = "APP-查询地点车间历史")
    @GetMapping(value = "getLsFddcj")
    public HttpResult getLsFddcj(@RequestParam(value = "sqsxid",defaultValue = "0") @ApiParam("申请事项id") String sqsxid){

        return HttpResult.ok(spcyXcjcBjddMapper.selectHistoryFddcjBySqsxId(sqsxid));
    }

    @Log(title = "APP-查询具体库历史")
    @ApiOperation(value = "APP-查询具体库历史")
    @GetMapping(value = "getLsFjtk")
    public HttpResult getLsFjtk(@RequestParam(value = "sqsxid",defaultValue = "0") @ApiParam("申请事项id") String sqsxid){

        return HttpResult.ok(spcyXcjcBjddMapper.selectHistoryFjtkBySqsxId(sqsxid));
    }

    @Log(title = "现场检查笔记-文件检查情况-添加")
    @ApiOperation(value = "现场检查笔记-文件检查情况-添加")
    @PostMapping(value = "addJcqk")
    public HttpResult addJcqk(@RequestBody XcjcWjjcqkAddVo paramVo){
        if (StringUtils.isBlank(paramVo.getSqsxid())){
            return HttpResult.error("事项参数为空！");
        }

        SpcyXcjcWjjcqk bean = new SpcyXcjcWjjcqk();
        bean.setFsqsxid(paramVo.getSqsxid());
        bean.setFuserid(GetCurrentUserUtils.getCurrentUserId());
        bean.setFwjfl(paramVo.getFwjfl());
        bean.setFwjm(paramVo.getFwjm());
        bean.setFwjbh(paramVo.getFwjbh());
        bean.setFqyzxqk(paramVo.getFqyzxqk());
        spcyXcjcWjjcqkService.save(bean);

        return HttpResultVo.HttpResultSave(bean, "添加成功");
    }

    @Log(title = "现场检查笔记-文件检查情况-修改")
    @ApiOperation(value = "现场检查笔记-文件检查情况-修改")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id",value = "文件检查情况id"),
            @ApiImplicitParam(name = "fwjfl",value = "文件分类"),
            @ApiImplicitParam(name = "fwjm",value = "文件名"),
            @ApiImplicitParam(name = "fwjbh",value = "文件编号"),
            @ApiImplicitParam(name = "fqyzxqk",value = "企业执行情况")
    })
    @PostMapping(value = "updateJcqk")
    public HttpResult updateJcqk(@RequestBody HashMap<String,Object> map) {
        String id = "";
        try {
            id = map.get("id").toString();
        }catch (Exception e){
            return HttpResult.error("参数丢失！");
        }

        if (StringUtils.isBlank(id)){
            return HttpResult.error("参数为空！");
        }

        SpcyXcjcWjjcqk updateBean = spcyXcjcWjjcqkService.getById(id);
        if (updateBean == null){
            return HttpResult.error("没有想匹配的数据！");
        }

        // 修改
        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        try {
            BeanUtils.populate(updateBean, map);
        } catch (IllegalAccessException e) {
            log.error("出错-文件检查情况-修改【非法访问异常】,{}",e.getMessage());
        } catch (InvocationTargetException e) {
            log.error("出错-文件检查情况-修改【调用TargetException】,{}",e.getMessage());
        }

        spcyXcjcWjjcqkService.updateById(updateBean);

        return HttpResultVo.HttpResultSave(updateBean, "保存成功");
    }

    @Log(title = "现场检查笔记-文件检查情况-删除")
    @ApiOperation(value = "现场检查笔记-文件检查情况-删除")
    @DeleteMapping(value = "deleteJcqk")
    public HttpResult<?> deleteJcqk(@RequestParam(value = "id",defaultValue = "") @ApiParam("文件检查情况id") String id) {
        if (StringUtils.isBlank(id)){
            return HttpResult.error("参数为空！");
        }

        SpcyXcjcWjjcqk bean = spcyXcjcWjjcqkService.getById(id);
        if (bean == null){
            return HttpResult.error("没有相匹配的数据！");
        }

        spcyXcjcWjjcqkService.removeById(id);

        String operContent = "删除现场检查笔记-文件检查情况信息：";
        operContent += LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(bean));
        spcyLogService.addLogs(LogConstant.LOGS_SQSX_FG_XCJC, operContent, id, "现场检查笔记-文件检查情况-删除", SpcyConstant.LOGS_SYS_TYPE_SC);

        return HttpResult.ok("删除成功");
    }


    @Log(title = "现场检查笔记-检查地点-列表查询")
//    @ApiOperation(value = "现场检查笔记-检查地点-列表查询")
    @GetMapping(value = "queryJcddList")
    public HttpResult queryJcddList(@RequestParam(value = "sqsxid",defaultValue = "") @ApiParam("申请事项id") String sqsxid){
        if (StringUtils.isBlank(sqsxid)){
            return HttpResult.error("参数为空！");
        }

        if (spcyService.verifyNowUserIsXpryZz(sqsxid)) {
            return HttpResult.ok(spcyXcjcBjddService.list(new LambdaQueryWrapper<SpcyXcjcBjdd>()
                    .eq(SpcyXcjcBjdd::getFsqsxid, sqsxid)
                    .orderByAsc(SpcyXcjcBjdd::getCreateTime)));
        }else {
            return HttpResult.ok(spcyXcjcBjddService.list(new LambdaQueryWrapper<SpcyXcjcBjdd>()
                    .eq(SpcyXcjcBjdd::getFsqsxid, sqsxid)
                    .eq(SpcyXcjcBjdd::getFcjrid, GetCurrentUserUtils.getCurrentUserId())
                    .orderByAsc(SpcyXcjcBjdd::getCreateTime)));
        }
    }

    @Log(title = "现场检查笔记-检查目录-列表查询")
    @ApiOperation(value = "现场检查笔记-检查目录-列表查询")
    @GetMapping(value = "queryJcmlList")
    public HttpResult queryJcmlList(@RequestParam(value = "sqsxid",defaultValue = "") @ApiParam("申请事项id") String sqsxid,
                                    @RequestParam(value = "fuserid",defaultValue = "") @ApiParam("用户id") String fuserid){
        if (StringUtils.isBlank(sqsxid)){
            return HttpResult.error("参数为空！");
        }

        if (StringUtils.isBlank(fuserid)) {
            return HttpResultVo.HttpResultGet(spcyXcjcBjmlService.selectMlListByFsqsxId(sqsxid));
        }else {
            return HttpResultVo.HttpResultGet(spcyXcjcBjmlService.selectMlListByFsqsxIdAndFuserid(sqsxid, fuserid));
        }
    }

    @Log(title = "获取APP-检查笔记地点目录-列表查询")
    @ApiOperation(value = "获取APP-检查笔记地点目录-列表查询")
    @GetMapping(value = "getAppJcdd")
    public HttpResult getAppJcdd(@RequestParam(value = "sqsxid",defaultValue = "") @ApiParam("申请事项id") String sqsxid){
        if (StringUtils.isBlank(sqsxid)){
            return HttpResult.error("参数为空！");
        }

        return HttpResultVo.HttpResultGet(spcyXcjcBjmlService.selectMlListByFsqsxIdAndFuserid(sqsxid, GetCurrentUserUtils.getCurrentUserId()));
    }

    @Log(title = "现场检查笔记-检查地点及文件详情列表-根据事项id")
    @ApiOperation(value = "现场检查笔记-检查地点及文件详情列表-根据事项id")
    @GetMapping(value = "jcbjInfo")
    public HttpResult jcbjInfo(@RequestParam(value = "sqsxid",defaultValue = "") @ApiParam("申请事项id") String sqsxid,
                               @RequestParam(value = "fmlid",defaultValue = "") @ApiParam("目录id") String fmlid,
                               @RequestParam(value = "fuserid",defaultValue = "") @ApiParam("用户id") String fuserid){
        if (StringUtils.isBlank(sqsxid)){
            return HttpResult.error("事项参数为空！");
        }

        List<SpcyXcjcBjdd> ddList = new ArrayList<>();
        if (spcyService.verifyNowUserIsXpryZz(sqsxid)) {
            if (StringUtils.isBlank(fmlid)){
                if (StringUtils.isBlank(fuserid)) {
                    ddList = spcyXcjcBjddService.list(new LambdaQueryWrapper<SpcyXcjcBjdd>()
                            .eq(SpcyXcjcBjdd::getFsqsxid, sqsxid)
                            .orderByAsc(SpcyXcjcBjdd::getCreateTime));
                }else {
                    ddList = spcyXcjcBjddService.list(new LambdaQueryWrapper<SpcyXcjcBjdd>()
                            .eq(SpcyXcjcBjdd::getFsqsxid, sqsxid)
                            .eq(SpcyXcjcBjdd::getFcjrid, fuserid)
                            .orderByAsc(SpcyXcjcBjdd::getCreateTime));
                }
            }else {
                ddList = spcyXcjcBjddService.list(new LambdaQueryWrapper<SpcyXcjcBjdd>()
                        .eq(SpcyXcjcBjdd::getFmlid, fmlid)
                        .orderByAsc(SpcyXcjcBjdd::getCreateTime));
            }

        }else {
            if (StringUtils.isBlank(fmlid)) {
                if (StringUtils.isBlank(fuserid)){
                    ddList = spcyXcjcBjddService.list(new LambdaQueryWrapper<SpcyXcjcBjdd>()
                            .eq(SpcyXcjcBjdd::getFsqsxid, sqsxid)
                            .eq(SpcyXcjcBjdd::getFcjrid, GetCurrentUserUtils.getCurrentUserId())
                            .orderByAsc(SpcyXcjcBjdd::getCreateTime));
                }else {
                    ddList = spcyXcjcBjddService.list(new LambdaQueryWrapper<SpcyXcjcBjdd>()
                            .eq(SpcyXcjcBjdd::getFsqsxid, sqsxid)
                            .eq(SpcyXcjcBjdd::getFcjrid, fuserid)
                            .orderByAsc(SpcyXcjcBjdd::getCreateTime));
                }
            }else {
                ddList = spcyXcjcBjddService.list(new LambdaQueryWrapper<SpcyXcjcBjdd>()
                        .eq(SpcyXcjcBjdd::getFmlid, fmlid)
                        .eq(SpcyXcjcBjdd::getFcjrid, GetCurrentUserUtils.getCurrentUserId())
                        .orderByAsc(SpcyXcjcBjdd::getCreateTime));
            }
        }

        List<SpcySqsxWjjl> wjList = spcySqsxWjjlService.list(new LambdaQueryWrapper<SpcySqsxWjjl>()
                .eq(SpcySqsxWjjl::getFpid, sqsxid)
                .eq(SpcySqsxWjjl::getFwjlx1, WjmlConstant.xcjcbl_jcgcjl)
                .isNotNull(SpcySqsxWjjl::getFwjlx2)
                .orderByAsc(SpcySqsxWjjl::getCreateTime));

        List<Map> resultMapList = new ArrayList<>(ddList.size());
        for (SpcyXcjcBjdd dd : ddList){
            Map dataMap = new HashMap();
            dataMap.put("dd", dd);

            dataMap.put("wjList", wjList.stream()
                    .filter( f-> f.getFwjlx2().equals(dd.getId()))
                    .collect(Collectors.toList()));

            resultMapList.add(dataMap);
        }

        return HttpResult.ok(resultMapList);
    }

    @Log(title = "获取app-检查笔记详情列表-根据事项id")
    @ApiOperation(value = "获取app-检查笔记详情列表-根据事项id")
    @GetMapping(value = "appBjxq")
    public HttpResult appBjxq(@RequestParam(value = "sqsxid",defaultValue = "") @ApiParam("申请事项id") String sqsxid,
                              @RequestParam(value = "fmlid",defaultValue = "") @ApiParam("目录id") String fmlid,
                              @RequestParam(value = "fuserid",defaultValue = "") @ApiParam("用户id") String fuserid){
        if (StringUtils.isBlank(sqsxid)){
            return HttpResult.error("事项参数为空！");
        }

        List<SpcyXcjcBjdd> ddList = new ArrayList<>();

        if (StringUtils.isBlank(fmlid)) {
            if (StringUtils.isBlank(fuserid)){
                ddList = spcyXcjcBjddService.list(new LambdaQueryWrapper<SpcyXcjcBjdd>()
                        .eq(SpcyXcjcBjdd::getFsqsxid, sqsxid)
                        .eq(SpcyXcjcBjdd::getFcjrid, GetCurrentUserUtils.getCurrentUserId())
                        .orderByAsc(SpcyXcjcBjdd::getCreateTime));
            }else {
                ddList = spcyXcjcBjddService.list(new LambdaQueryWrapper<SpcyXcjcBjdd>()
                        .eq(SpcyXcjcBjdd::getFsqsxid, sqsxid)
                        .eq(SpcyXcjcBjdd::getFcjrid, fuserid)
                        .orderByAsc(SpcyXcjcBjdd::getCreateTime));
            }
        }else {
            ddList = spcyXcjcBjddService.list(new LambdaQueryWrapper<SpcyXcjcBjdd>()
                    .eq(SpcyXcjcBjdd::getFmlid, fmlid)
                    .eq(SpcyXcjcBjdd::getFcjrid, GetCurrentUserUtils.getCurrentUserId())
                    .orderByAsc(SpcyXcjcBjdd::getCreateTime));
        }

        List<SpcySqsxWjjl> wjList = spcySqsxWjjlService.list(new LambdaQueryWrapper<SpcySqsxWjjl>()
                .eq(SpcySqsxWjjl::getFpid, sqsxid)
                .eq(SpcySqsxWjjl::getFwjlx1, WjmlConstant.xcjcbl_jcgcjl)
                .isNotNull(SpcySqsxWjjl::getFwjlx2)
                .orderByAsc(SpcySqsxWjjl::getCreateTime));

        List<Map> resultMapList = new ArrayList<>(ddList.size());
        for (SpcyXcjcBjdd dd : ddList){
            Map dataMap = new HashMap();
            dataMap.put("dd", dd);

            dataMap.put("wjList", wjList.stream()
                    .filter( f-> f.getFwjlx2().equals(dd.getId()))
                    .collect(Collectors.toList()));

            resultMapList.add(dataMap);
        }

        return HttpResult.ok(resultMapList);
    }

    @Log(title = "获取现场检查笔记-文件列表-根据地点id")
    @ApiOperation(value = "获取现场检查笔记-文件列表-根据地点id")
    @GetMapping(value = "jcbjWjById")
    public HttpResult jcbjWjById(@RequestParam(value = "id",defaultValue = "") @ApiParam("检查地点id") String id){
        if (StringUtils.isBlank(id)){
            return HttpResult.error("参数为空！");
        }

        return HttpResultVo.HttpResultList(spcySqsxWjjlService.list(new LambdaQueryWrapper<SpcySqsxWjjl>()
                .eq(SpcySqsxWjjl::getFwjlx2, id)
                .orderByAsc(SpcySqsxWjjl::getCreateTime)));
    }

    @Log(title = "获取现场检查笔记-内容及文件详细-根据地点id")
    @ApiOperation(value = "获取现场检查笔记-内容及文件详细-根据地点id")
    @GetMapping(value = "jcbjxqById")
    public HttpResult jcbjxqById(@RequestParam(value = "id",defaultValue = "") @ApiParam("检查地点id") String id){
        if (StringUtils.isBlank(id)){
            return HttpResult.error("参数为空！");
        }

        Map dataMap = new HashMap();
        dataMap.put("dd", spcyXcjcBjddService.getById(id));
        dataMap.put("wjList", spcySqsxWjjlService.list(new LambdaQueryWrapper<SpcySqsxWjjl>()
                .eq(SpcySqsxWjjl::getFwjlx2, id)
                .orderByAsc(SpcySqsxWjjl::getCreateTime)));

        return HttpResult.ok(dataMap);
    }

    @Log(title = "现场检查笔记-检查地点-添加")
    @ApiOperation(value = "现场检查笔记-检查地点-添加")
    @PostMapping(value = "addJcdd")
    public HttpResult addJcdd(@RequestBody XcjcBjddAddVo bjddAddVo){
        if (StringUtils.isBlank(bjddAddVo.getFmlid())){
            return HttpResult.error("目录参数为空！");
        }

        SpcyXcjcBjdd bjdd = new SpcyXcjcBjdd();
        bjdd.setId(GetUuIdUtils.ReplaceUuId());
        bjdd.setFsqsxid(bjddAddVo.getSqsxid());
        bjdd.setFmlid(bjddAddVo.getFmlid());
        bjdd.setFddmc(bjddAddVo.getFddmc());

        XcjcBjmlVo ml = spcyXcjcBjmlService.selectMlBeanById(bjdd.getFmlid());
        if (ml == null){
            return HttpResult.error("没有对应的目录数据！");
        }
        bjdd.setFsqsxid(ml.getFsqsxid());
        bjdd.setFddmc(ml.getFddcj());

        bjdd.setFmsnr(bjddAddVo.getFmsnr());
        bjdd.setFjtk(bjddAddVo.getFjtk());
        bjdd.setFcjrid(GetCurrentUserUtils.getCurrentUserId());
        bjdd.setFcjrxm(GetCurrentUserUtils.getCurrentUserName());
        spcyXcjcBjddService.save(bjdd);

        return HttpResultVo.HttpResultSave(bjdd, "保存成功");
    }

    @Log(title = "APP端-现场检查笔记-检查地点-添加")
    @ApiOperation(value = "APP端-现场检查笔记-检查地点-添加")
    @PostMapping(value = "addAppJcdd")
    public HttpResult addAppJcdd(@RequestBody XcjcBjddAddVo paramVo){
        if (StringUtils.isBlank(paramVo.getSqsxid())){
            return HttpResult.error("事项参数为空！");
        }

        SpcyXcjcBjdd bjdd = new SpcyXcjcBjdd();
        bjdd.setId(GetUuIdUtils.ReplaceUuId());
        bjdd.setFsqsxid(paramVo.getSqsxid());
        bjdd.setFddmc(paramVo.getFddmc());

        String userid = GetCurrentUserUtils.getCurrentUserId();
        String userxm = GetCurrentUserUtils.getCurrentUserName();
        if (StringUtils.isNotBlank(paramVo.getFddmc())) {
            List<XcjcBjmlVo> mlList = spcyXcjcBjmlService.selectBjmlByFsqsxIdAndFddcj(paramVo.getSqsxid(), paramVo.getFddmc(), userid);
            if (CollectionUtils.isNotEmpty(mlList)) {
                bjdd.setFmlid(mlList.get(0).getId());
            } else {
                SpcyXcjcBjml bjml = new SpcyXcjcBjml();
                bjml.setId(GetUuIdUtils.ReplaceUuId());
                bjml.setFddcj(paramVo.getFddmc());
                bjml.setFsqsxid(paramVo.getSqsxid());
                bjml.setFuserid(userid);
                spcyXcjcBjmlService.save(bjml);
                bjdd.setFmlid(bjml.getId());
            }
        }

        bjdd.setFmsnr(paramVo.getFmsnr());
        bjdd.setFjtk(paramVo.getFjtk());
        bjdd.setFcjrid(userid);
        bjdd.setFcjrxm(userxm);
        spcyXcjcBjddService.save(bjdd);

        return HttpResultVo.HttpResultSave(bjdd, "保存成功");
    }

    @Log(title = "现场检查笔记-检查地点-修改")
    @ApiOperation(value = "现场检查笔记-检查地点-修改")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id",value = ""),
            @ApiImplicitParam(name = "fddmc",value = "地点名称"),
            @ApiImplicitParam(name = "fmsnr",value = "描述内容"),
            @ApiImplicitParam(name = "fjtk",value = "具体库")
    })
    @PostMapping(value = "updateJcdd")
    public HttpResult updateJcdd(@RequestBody HashMap<String,Object> map) {
        String id = "";
        try {
            id = map.get("id").toString();
        }catch (Exception e){
            return HttpResult.error("参数丢失！");
        }

        if (StringUtils.isBlank(id)){
            return HttpResult.error("参数为空！");
        }

        SpcyXcjcBjdd updateBean = spcyXcjcBjddService.getById(id);
        if (updateBean == null){
            return HttpResult.error("没有想匹配的数据！");
        }

        // 修改
        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        try {
            BeanUtils.populate(updateBean, map);
        } catch (IllegalAccessException e) {
            log.error("出错-现场检查笔记-修改【非法访问异常】,{}",e.getMessage());
        } catch (InvocationTargetException e) {
            log.error("出错-现场检查笔记-修改【调用TargetException】,{}",e.getMessage());
        }
        spcyXcjcBjddService.updateById(updateBean);

        return HttpResult.ok("保存成功");
    }

    @Log(title = "APP端-现场检查笔记-检查地点-修改")
    @ApiOperation(value = "APP端-现场检查笔记-检查地点-修改")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id",value = ""),
            @ApiImplicitParam(name = "fddmc",value = "地点名称"),
            @ApiImplicitParam(name = "fmsnr",value = "描述内容"),
            @ApiImplicitParam(name = "fjtk",value = "具体库")
    })
    @PostMapping(value = "updateAppJcdd")
    public HttpResult updateAppJcdd(@RequestBody HashMap<String,Object> map) {
        String id = "";
        try {
            id = map.get("id").toString();
        }catch (Exception e){
            return HttpResult.error("参数丢失！");
        }

        if (StringUtils.isBlank(id)){
            return HttpResult.error("参数为空！");
        }

        SpcyXcjcBjdd updateBean = spcyXcjcBjddService.getById(id);
        if (updateBean == null){
            return HttpResult.error("没有想匹配的数据！");
        }

        String oldDdmc = updateBean.getFddmc();
        String fmlid = updateBean.getFmlid();
        String userid = updateBean.getFcjrid();
        String sqsxid = updateBean.getFsqsxid();

        // 修改
        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        try {
            BeanUtils.populate(updateBean, map);
        } catch (IllegalAccessException e) {
            log.error("出错-现场检查笔记-修改【非法访问异常】,{}",e.getMessage());
        } catch (InvocationTargetException e) {
            log.error("出错-现场检查笔记-修改【调用TargetException】,{}",e.getMessage());
        }

        String newDdmc = updateBean.getFddmc();

        if (StringUtils.isNotBlank(newDdmc) && StringUtils.isNotBlank(fmlid)) {
            if (StringUtils.isBlank(oldDdmc) || !newDdmc.equals(oldDdmc)) {
                LambdaUpdateWrapper<SpcyXcjcBjml> updateWrapperMl = new LambdaUpdateWrapper<>();
                updateWrapperMl.eq(SpcyXcjcBjml::getId, fmlid);

                SpcyXcjcBjml updateMl = new SpcyXcjcBjml();
                updateMl.setFddcj(newDdmc);
                spcyXcjcBjmlService.update(updateMl, updateWrapperMl);
            }
        }

        if (StringUtils.isBlank(fmlid) && StringUtils.isNotBlank(newDdmc)){
            List<XcjcBjmlVo> mlList = spcyXcjcBjmlService.selectBjmlByFsqsxIdAndFddcj(sqsxid, newDdmc, userid);
            if (CollectionUtils.isNotEmpty(mlList)) {
                updateBean.setFmlid(mlList.get(0).getId());
            } else {
                SpcyXcjcBjml bjml = new SpcyXcjcBjml();
                bjml.setId(GetUuIdUtils.ReplaceUuId());
                bjml.setFddcj(newDdmc);
                bjml.setFsqsxid(sqsxid);
                bjml.setFuserid(userid);
                spcyXcjcBjmlService.save(bjml);
                updateBean.setFmlid(bjml.getId());
            }
        }

        spcyXcjcBjddService.updateById(updateBean);

        return HttpResult.ok("保存成功");
    }

    @Log(title = "现场检查笔记-检查地点-删除")
    @ApiOperation(value = "现场检查笔记-检查地点-删除")
    @DeleteMapping(value = "deleteJcdd")
    public HttpResult<?> deleteJcdd(@RequestParam(value = "id",defaultValue = "") @ApiParam("检查地点id") String id) {
        if (StringUtils.isEmpty(id)){
            return HttpResult.error("参数为空");
        }

        SpcyXcjcBjdd updateBean = spcyXcjcBjddService.getById(id);
        if (updateBean == null){
            return HttpResult.error("没有想匹配的模板数据！");
        }

        boolean flag = spcyXcjcBjddService.removeById(id);
        if (flag) {
            List<SpcySqsxWjjl> wjList = spcySqsxWjjlService.list(new LambdaQueryWrapper<SpcySqsxWjjl>()
                    .eq(SpcySqsxWjjl::getFwjlx2, id));
            if (CollectionUtils.isNotEmpty(wjList)) {

                spcySqsxWjjlService.remove(new LambdaQueryWrapper<SpcySqsxWjjl>()
                        .eq(SpcySqsxWjjl::getFwjlx2, id));

                for (SpcySqsxWjjl wjjl : wjList) {
                    if (StringUtils.isNotBlank(wjjl.getFcflj())) {
                        localFileUploadUtils.deleteLocalFileUrl(wjjl.getFcflj());
                    }

                    if (StringUtils.isNotBlank(wjjl.getFjdcflj())) {
                        localFileUploadUtils.deleteLocalFileUrl(wjjl.getFjdcflj());
                    }
                }
            }

            String operContent = "删除现场检查笔记-检查地点信息：";
            operContent += LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(updateBean));
            spcyLogService.addLogs(LogConstant.LOGS_SQSX_FG_XCJC, operContent, id, "现场检查笔记-检查地点-删除", SpcyConstant.LOGS_SYS_TYPE_SC);
        }

        return HttpResult.ok("删除成功");
    }

    @Log(title = "现场检查笔记-检查地方车间目录-添加")
    @ApiOperation(value = "现场检查笔记-检查地方车间目录-添加")
    @PostMapping(value = "addJcml")
    public HttpResult addJcml(@RequestBody XcjcBjmlAddVo paramVo){
        if (StringUtils.isBlank(paramVo.getSqsxid())){
            return HttpResult.error("事项参数为空！");
        }
        if (StringUtils.isBlank(paramVo.getFddcj())){
            return HttpResult.error("内容为空！");
        }

        SpcyXcjcBjml bjml = new SpcyXcjcBjml();
        bjml.setId(GetUuIdUtils.ReplaceUuId());
        bjml.setFddcj(paramVo.getFddcj());
        bjml.setFsqsxid(paramVo.getSqsxid());
        bjml.setFuserid(GetCurrentUserUtils.getCurrentUserId());
        spcyXcjcBjmlService.save(bjml);

        return HttpResultVo.HttpResultSave(bjml, "保存成功");
    }

    @Log(title = "现场检查笔记-检查地方车间目录-修改")
    @ApiOperation(value = "现场检查笔记-检查地方车间目录-修改")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id",value = "目录id"),
            @ApiImplicitParam(name = "fddcj",value = "地点车间")
    })
    @PostMapping(value = "updateJcml")
    public HttpResult updateJcml(@RequestBody HashMap<String,Object> map) {
        String id = "";
        try {
            id = map.get("id").toString();
        }catch (Exception e){
            return HttpResult.error("参数丢失！");
        }

        if (StringUtils.isBlank(id)){
            return HttpResult.error("参数为空！");
        }

        SpcyXcjcBjml updateBean = spcyXcjcBjmlService.getById(id);
        if (updateBean == null){
            return HttpResult.error("没有想匹配的数据！");
        }

        String oldMlMc = updateBean.getFddcj();

        // 修改
        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        try {
            BeanUtils.populate(updateBean, map);
        } catch (IllegalAccessException e) {
            log.error("出错-检查地方车间目录-修改【非法访问异常】,{}",e.getMessage());
        } catch (InvocationTargetException e) {
            log.error("出错-检查地方车间目录-修改【调用TargetException】,{}",e.getMessage());
        }

        /** 同步地点 */
        if (StringUtils.isNotBlank(updateBean.getFddcj())) {
            if (StringUtils.isBlank(oldMlMc)  || !oldMlMc.equals(updateBean.getFddcj())) {
                LambdaUpdateWrapper<SpcyXcjcBjdd> updateWrapper = new LambdaUpdateWrapper<>();
                updateWrapper.eq(SpcyXcjcBjdd::getFmlid, updateBean.getId());

                SpcyXcjcBjdd updateDd = new SpcyXcjcBjdd();
                updateDd.setFddmc(updateBean.getFddcj());

                spcyXcjcBjddService.update(updateDd, updateWrapper);
            }
        }

        spcyXcjcBjmlService.updateById(updateBean);

        return HttpResultVo.HttpResultSave(updateBean, "保存成功");
    }

    @Log(title = "现场检查笔记-检查地方车间目录-删除")
    @ApiOperation(value = "现场检查笔记-检查地方车间目录-删除")
    @DeleteMapping(value = "deleteJcml")
    public HttpResult<?> deleteJcml(@RequestParam(value = "id",defaultValue = "") @ApiParam("目录id") String id) {

        spcyXcjcBjmlService.removeById(id);

        List<SpcyXcjcBjdd> list = spcyXcjcBjddService.list(new LambdaQueryWrapper<SpcyXcjcBjdd>()
                .eq(SpcyXcjcBjdd::getFmlid, id));
        for (SpcyXcjcBjdd bean : list){
            List<SpcySqsxWjjl> wjList = spcySqsxWjjlService.list(new LambdaQueryWrapper<SpcySqsxWjjl>()
                    .eq(SpcySqsxWjjl::getFwjlx2, bean.getId()));
            if (CollectionUtils.isNotEmpty(wjList)) {

                spcySqsxWjjlService.remove(new LambdaQueryWrapper<SpcySqsxWjjl>()
                        .eq(SpcySqsxWjjl::getFwjlx2, bean.getId()));

                for (SpcySqsxWjjl wjjl : wjList) {
                    if (StringUtils.isNotBlank(wjjl.getFcflj())) {
                        localFileUploadUtils.deleteLocalFileUrl(wjjl.getFcflj());
                    }

                    if (StringUtils.isNotBlank(wjjl.getFjdcflj())) {
                        localFileUploadUtils.deleteLocalFileUrl(wjjl.getFjdcflj());
                    }
                }
            }

            String operContent = "删除现场检查笔记-检查地点信息：";
            operContent += LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(bean));
            spcyLogService.addLogs(LogConstant.LOGS_SQSX_FG_XCJC, operContent, id, "现场检查笔记-检查地点-删除", SpcyConstant.LOGS_SYS_TYPE_SC);
        }

        return HttpResult.ok("删除成功");
    }

    @Log(title = "上传-现场检查笔记文件-根据检查地点")
    @ApiOperation(value = "上传-现场检查笔记文件-根据检查地点")
    @PostMapping(value = "uploadDdBjFile")
    public HttpResult uploadDdBjFile(@RequestPart("file") MultipartFile[] uploadFile,
                                     @RequestParam(value = "id",defaultValue = "") @ApiParam("检查地点id") String jcddid,
                                     @RequestParam(value = "wjbs",defaultValue = "") @ApiParam("文件标识") String wjbs,
                                     @RequestParam(value = "fwjxsmc",defaultValue = "") @ApiParam("自定义文件显示名（不带后缀）") String fwjxsmc){
        if (uploadFile == null || uploadFile.length == 0) {
            return HttpResult.error("上传失败,请先选择文件！");
        }

        if (StringUtils.isBlank(jcddid)){
            return HttpResult.error("地点参数为空！");
        }

//        SpcyXcjcBjdd bjdd = spcyXcjcBjddService.getById(jcddid);
        String sqsxid = spcyXcjcBjddMapper.selectFsqsxidById(jcddid);
        if (StringUtils.isBlank(sqsxid)){
            return HttpResult.error("没有相匹配地点数据！");
        }

        /*SpcyXcjcBjms bjms = new SpcyXcjcBjms();
        bjms.setId(GetUuIdUtils.ReplaceUuId());
        bjms.setFddid(jcddid);
        bjms.setFsqsxid(bjdd.getFsqsxid());
        bjms.setFmsnr(fmsnr);
        spcyXcjcBjmsService.save(bjms);*/

        if (StringUtils.isNotBlank(fwjxsmc)){
            String validFileName = StringPatternUtils.isValidFileName(fwjxsmc);
            if (StringUtils.isNotBlank(validFileName)){
                return HttpResult.error("文件名不能存在特殊字符："+validFileName);
            }
        }else {
            fwjxsmc = Util.getWjmcNoHz(uploadFile[0].getOriginalFilename());
        }

        String wjlx = Util.getWjHz(uploadFile[0].getOriginalFilename());
        String wjid = GetUuIdUtils.ReplaceUuId();
        initWjjlService.uploadCommonFile(uploadFile, sqsxid, WjmlConstant.xcjcbl_jcgcjl, jcddid, fwjxsmc, wjid,false, wjbs);

        Map resultMap = new HashMap();
//        resultMap.put("msid", bjms.getId());
        resultMap.put("wjid", wjid);

        resultMap.put("fwjxsmc", fwjxsmc);
        resultMap.put("fwjlx", wjlx);
        resultMap.put("wjbs", wjbs);
        return HttpResultVo.HttpResultSave(resultMap,"上传成功");
    }

    @Log(title = "上传-现场检查笔记文件列表-根据检查地点id")
    @ApiOperation(value = "上传-现场检查笔记文件列表-根据检查地点id")
    @PostMapping(value = "uploadBjFiles")
    public HttpResult uploadBjFiles(HttpServletRequest request,
                                    @RequestParam(value = "id",defaultValue = "") @ApiParam("检查地点id") String jcddid){

        if (StringUtils.isBlank(jcddid)){
            return HttpResult.error("地点参数为空！");
        }

        String sqsxid = spcyXcjcBjddMapper.selectFsqsxidById(jcddid);
        if (StringUtils.isBlank(sqsxid)){
            return HttpResult.error("没有相匹配地点数据！");
        }

        try {
            String error = initWjjlService.uploadCommonParts(request.getParts(), sqsxid, WjmlConstant.xcjcbl_jcgcjl, jcddid, "", GetUuIdUtils.ReplaceUuId(), false, "");
            if (error != null){
                return HttpResult.error("上传失败！"+ error);
            }
        }catch (Exception e){
            return HttpResult.error("上传失败！");
        }

        return HttpResultVo.HttpResultSave(null,"上传成功");
    }

    @Log(title = "APP上传-现场检查笔记文件及内容")
    @ApiOperation(value = "APP上传-现场检查笔记文件及内容")
    @PostMapping(value = "upAppBj")
    public HttpResult upAppBj(HttpServletRequest request,
                              XcjcBjddAddVo paramVo){

        if (StringUtils.isBlank(paramVo.getSqsxid())){
            return HttpResult.error("事项参数为空！");
        }

        SpcyXcjcBjdd bjdd = initAddBjdd(paramVo);

        try {
            String error = initWjjlService.uploadCommonParts(request.getParts(), bjdd.getFsqsxid(), WjmlConstant.xcjcbl_jcgcjl, bjdd.getId(), "","", false, "");
            if (error != null){
                return HttpResult.error("上传失败！"+ error);
            }
        }catch (Exception e){
            return HttpResult.error("上传失败！");
        }

        Map resultMap = new HashMap();
        resultMap.put("bj", bjdd);

        return HttpResultVo.HttpResultSave(resultMap,"上传成功");
    }

    /** 初始化笔记地点信息 */
    private SpcyXcjcBjdd initAddBjdd(XcjcBjddAddVo paramVo){
        String userid = GetCurrentUserUtils.getCurrentUserId();
        String userxm = GetCurrentUserUtils.getCurrentUserName();

        SpcyXcjcBjdd bjdd = new SpcyXcjcBjdd();
        bjdd.setId(GetUuIdUtils.ReplaceUuId());
        bjdd.setFsqsxid(paramVo.getSqsxid());

        bjdd.setFddmc(paramVo.getFddmc());
        if (StringUtils.isBlank(paramVo.getFmlid())) {
            if (StringUtils.isNotBlank(paramVo.getFddmc())) {
                List<XcjcBjmlVo> mlList = spcyXcjcBjmlService.selectBjmlByFsqsxIdAndFddcj(paramVo.getSqsxid(), paramVo.getFddmc(), userid);
                if (CollectionUtils.isNotEmpty(mlList)) {
                    bjdd.setFmlid(mlList.get(0).getId());
                } else {
                    SpcyXcjcBjml bjml = new SpcyXcjcBjml();
                    bjml.setId(GetUuIdUtils.ReplaceUuId());
                    bjml.setFddcj(paramVo.getFddmc());
                    bjml.setFsqsxid(paramVo.getSqsxid());
                    bjml.setFuserid(userid);
                    spcyXcjcBjmlService.save(bjml);
                    bjdd.setFmlid(bjml.getId());
                }
            }
        } else {
            bjdd.setFmlid(paramVo.getFmlid());
            bjdd.setFddmc(spcyXcjcBjmlService.selectMlFddcjById(paramVo.getFmlid()));
            if (StringUtils.isBlank(bjdd.getFddmc())) {
                bjdd.setFddmc(paramVo.getFddmc());
            }
        }

        bjdd.setFmsnr(paramVo.getFmsnr());
        bjdd.setFjtk(paramVo.getFjtk());
        bjdd.setFcjrid(userid);
        bjdd.setFcjrxm(userxm);
        spcyXcjcBjddService.save(bjdd);

        return bjdd;
    }

    @Log(title = "生成构建现场检查记录文件")
    @ApiOperation(value = "生成构建现场检查记录文件")
    @GetMapping(value = "initXcjcjlFile")
    public HttpResult initXcjcjlFile(@RequestParam(value = "sqsxid",defaultValue = "") @ApiParam("申请事项id") String sqsxid){
        if (StringUtils.isBlank(sqsxid)){
            return HttpResult.error("事项参数为空！");
        }

        SpcySqsxBasic sqsx = spcySqsxBaiscMapper.selectSqsxInfoById(sqsxid);
        if (sqsx == null){
            return HttpResult.error("没有相匹配的事项数据！");
        }

        List<SpcySqsxXpjl> tyXpList = spcySqsxXpjlService.getTyXpXpjlList(sqsxid, "");
        for (SpcySqsxXpjl xpjl : tyXpList) {

            // 旧的文件需要删除
            List<SpcySqsxWjjl> oldWjList = spcySqsxWjjlService.list(new LambdaQueryWrapper<SpcySqsxWjjl>()
                    .eq(SpcySqsxWjjl::getFpid, sqsxid)
                    .eq(SpcySqsxWjjl::getFwjlx1, WjmlConstant.xcjcbl)
                    .eq(SpcySqsxWjjl::getFwjlx7, xpjl.getFuserid()));

            // 获取初始化文件
            String destWordUrl = "";
            try {
                destWordUrl = this.genXcjcbjSqsxMb(sqsxid, sqsx, xpjl.getFuserid(), xpjl.getFjcyxm() + "（"+xpjl.getFssk()+"）");
            } catch (Exception e) {
                log.error("生成构建现场检查记录文件错误：事项{}, msg: {}", sqsxid, e.getMessage());

                return HttpResult.error("模板逻辑错误");
            }

            if (StringUtils.isBlank(destWordUrl)){
                continue;
            }

            String wjHz = "";
            String wjccmc = "";

            String realurl = localFileUploadUtils.getTempFileRootUrl();
            String pdfname = GetUuIdUtils.NotReplaceUuId() + ".pdf"; // 文件名称
            String outPdfFileUrl = realurl + "/" + pdfname;
            String uploadSxFileUrl = ""; // 上传文件路径

            // 转pdf
            boolean pdfFlag = WordPdfUtils.wordToPdf(destWordUrl, outPdfFileUrl);
            if (!pdfFlag){
                wjHz = Util.getWjHz(destWordUrl);
                wjccmc = GetUuIdUtils.NotReplaceUuId() + wjHz;
                uploadSxFileUrl = destWordUrl;
            }else {
                // 验证pdf文件是否损坏
                if (WordPdfUtils.isPdfCorrupted(outPdfFileUrl)){
                    wjHz = Util.getWjHz(destWordUrl);
                    wjccmc = GetUuIdUtils.NotReplaceUuId() + wjHz;
                    uploadSxFileUrl = destWordUrl;
                }else {
                    wjHz = ".pdf";
                    wjccmc = pdfname;
                    uploadSxFileUrl = outPdfFileUrl;
                }
            }

            String cflj = "/" + DateConversionUtils.DateToMonthTwo(new Date()) + "/" + wjccmc;

            // minio文件上传
            boolean flag = busService.uploadMinioFileWjjl(cflj, uploadSxFileUrl);
            if (flag) {
                SpcySqsxWjjl initWj = spcySqsxWjjlService.initSaveSqsxWjjl(
                        sqsxid,
                        WjmlConstant.xcjcbl,
                        wjHz,
                        WjmlConstant.xcjcbl + "-" + xpjl.getFjcyxm(),
                        wjccmc,
                        cflj,
                        xpjl.getFuserid());
                if (initWj != null){
                    initWjjlService.deleteCommonSqsxFile(oldWjList);
                    localFileUploadUtils.deleteAbsFile(destWordUrl);
                    localFileUploadUtils.deleteAbsFile(outPdfFileUrl);
                }
            }else {
                log.error("生成构建现场检查记录文件-文件上传错误-错误：事项{}; 上传", sqsxid);
                return HttpResult.error("文件上传错误");
            }
        }

        return HttpResult.ok("操作成功");
    }

    /** 构建现场检查记录模板文件 */
    private String genXcjcbjSqsxMb(String sqsxid, SpcySqsxBasic sqsx, String jlryId, String jlry){
        if (sqsx == null) {
            sqsx = spcySqsxBaiscMapper.selectSqsxInfoById(sqsxid);
        }

        HashMap<String, Object> parMap = new HashMap<>();

        String fsxmc = "";
        String fqymc = "";
        String jcsj = "";
        String fjcdd = "";
        if (sqsx != null){
            fsxmc = sqsx.getFqymc();
            fqymc = sqsx.getFsxmc();
            if (sqsx.getFcykssj() != null){
                jcsj += DateConversionUtils.DateToStringNew(sqsx.getFcykssj());
            }
            if (sqsx.getFcyjssj() != null){
                jcsj += StringUtils.isNotBlank(jcsj) ? "至" : "";
                jcsj += DateConversionUtils.DateToStringNew(sqsx.getFcyjssj());
            }
            fjcdd = Optional.ofNullable(sqsx.getFjcdd()).orElseGet(()->"");
        }

        parMap.put("fsxmc", fsxmc);
        parMap.put("fqymc", fqymc);
        parMap.put("jcsj", jcsj);
        parMap.put("fjcdd", fjcdd);
        parMap.put("jlry", jlry);

        List<SpcySqsxWjjl> wjList = spcySqsxWjjlService.list(new LambdaQueryWrapper<SpcySqsxWjjl>()
                .eq(SpcySqsxWjjl::getFpid, sqsxid)
                .eq(SpcySqsxWjjl::getFwjlx1, WjmlConstant.xcjcbl_jcgcjl)
                .isNotNull(SpcySqsxWjjl::getFwjlx2));

        List<SpcyXcjcBjdd> jcddList = spcyXcjcBjddService.list(new LambdaQueryWrapper<SpcyXcjcBjdd>()
                .eq(SpcyXcjcBjdd::getFsqsxid, sqsxid)
                .eq(SpcyXcjcBjdd::getFcjrid, jlryId)
                .orderByAsc(SpcyXcjcBjdd::getFddmc));

        // 图片类型
        List<String> imageList = CollectionUtil.newArrayList(
                ".jpg",
                ".jpeg",
                ".png",
                ".gif",
                ".bmp",
                ".tiff",
                ".webp",
                ".svg"
        );

        List<Map> ddList = new ArrayList<>();
        for (SpcyXcjcBjdd bjdd : jcddList){
            Map ddMap = new HashMap();

            List<Map<String, PictureRenderData>> pictureList = new ArrayList<>();
            List<SpcySqsxWjjl> filterWjList = wjList.stream()
                    .filter(f -> f.getFwjlx2().equals(bjdd.getId()))
                    .collect(Collectors.toList());

            for (SpcySqsxWjjl wj : filterWjList){
                if (!imageList.contains(wj.getFwjlx())){
                    continue;
                }

                InputStream inputStream = spcyMinioUtils.getObject(localFileUploadUtils.getSqsxWjjlRelativeUrl(wj.getFcflj()));
                if (inputStream == null){
                    continue;
                }

                File file = localFileUploadUtils.asFile(inputStream, wj.getFwjlx());
                Map<String, PictureRenderData> picture = new HashMap<>();
                try {
                    picture.put("image", Pictures.ofStream(
                            new FileInputStream(file.getAbsoluteFile()), PictureType.suggestFileType(wj.getFwjlx()))
                            .size(200, 120).create());
                } catch (FileNotFoundException e) {
                    log.error("构建现场检查记录模板文件-错误：{}", e.getMessage());
                }
                pictureList.add(picture);
            }

            ddMap.put("dd", Optional.ofNullable(bjdd.getFddmc()).orElseGet(()->""));
            ddMap.put("jtk", Optional.ofNullable(bjdd.getFjtk()).orElseGet(()->""));
            ddMap.put("ms", Optional.ofNullable(bjdd.getFmsnr()).orElseGet(()->""));
            ddMap.put("images", pictureList);

            ddList.add(ddMap);
        }
        parMap.put("ddList", ddList);

        List<SpcyXcjcWjjcqk> jcqkList = spcyXcjcWjjcqkService.list(new LambdaQueryWrapper<SpcyXcjcWjjcqk>()
                .eq(SpcyXcjcWjjcqk::getFsqsxid, sqsxid)
                .eq(SpcyXcjcWjjcqk::getFuserid, jlryId));
        parMap.put("jcqkList", jcqkList);


        HackLoopTableRenderPolicy policy = new HackLoopTableRenderPolicy();
        ConfigureBuilder builder = Configure.builder();
        builder.bind("ddList", policy);
        builder.bind("jcqkList", policy);

        String orgUrl = localFileUploadUtils.getSysRootUrl() + "/" + WjmlConstant.Template_File_Export_XCJCBJ_JCJL;
        String destWordUrl = localFileUploadUtils.getTempFileRootUrl() + "/";
        destWordUrl += GetUuIdUtils.NotReplaceUuId() + Util.getWjHz(orgUrl);
        WordDocxReplaceUtils.docxConfigureReplace(orgUrl, destWordUrl, builder.useSpringEL(false).build(),parMap);
        return destWordUrl;
    }
}
